package com.citywithincity.redis.models;

import com.citywithincity.interfaces.IDestroyable;
import com.citywithincity.redis.utils.RedisUtil;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

public class RedisSetColModel implements IDestroyable {
	private String setKey;
	
	public RedisSetColModel(String setKey) {
		this.setKey = setKey;
	}

	@Override
	public void destroy() {
	}
	
	private byte[] getKey(Object userId){
		return new StringBuilder(setKey).append(':').append(userId).toString().getBytes();
	}

	
	public void edit(Pipeline jedis,Object userId, Object id){
		jedis.sadd(getKey(userId), String.valueOf(id).getBytes());
	}
	
	public void edit(Object userId, Object id ){
		
		Jedis jedis = null;
		try{
			jedis = RedisUtil.read();
			jedis.sadd(getKey(userId), String.valueOf(id).getBytes());
		}finally{
			if(jedis!=null)jedis.close();
		}
	}
	
	public void delete(Object userId,Object id){
		Jedis jedis = null;
		try{
			jedis = RedisUtil.read();
			jedis.srem(getKey(userId), String.valueOf(id).getBytes());
		}finally{
			if(jedis!=null)jedis.close();
		}
		
	}
	
	public void delete(Pipeline jedis,Object userId,Object id){
		jedis.srem(getKey(userId), String.valueOf(id).getBytes());
	}
	
	public boolean contains(Object userId,Object id){
		Jedis jedis = null;
		try{
			jedis = RedisUtil.read();
			return jedis.sismember(getKey(userId), String.valueOf(id).getBytes());
		}finally{
			if(jedis!=null)jedis.close();
		}
	}
	
	
	
}
